package com.softserveinc.lahire.server;

import java.awt.EventQueue;
import java.awt.TrayIcon.MessageType;
import java.io.IOException;
import java.util.Map;
import com.softserveinc.lahire.communicator.Communicator;
import com.softserveinc.lahire.configurator.Configurator;
import com.softserveinc.lahire.gui.TrayKeeper;

public abstract class Server {

	private static TrayKeeper trayKeeper;
	private static boolean serverRun;

	public static void main(String args[]) {

		startServer();

		// Start GUI in Event thread

		trayKeeper = new TrayKeeper();
		EventQueue.invokeLater(trayKeeper);

	}

	public static boolean isRunning() {
		return serverRun;
	}

	public static void exitServer() {
		try {
			Communicator.getInstance().stopMe();
			trayKeeper.stopMe();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static boolean startServer() {
		try {
			Configurator.getInstance();
			Communicator.getInstance().startMe();
			new Thread(Communicator.getInstance(), "Communicator").start();
			serverRun = true;
		} catch (IOException e) {
			serverRun = false;
		}
		return serverRun;
	}

	public static void stopServer() {
		try {
			Communicator.getInstance().stopMe();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			serverRun = false;
		}

	}
	
	public static void restartServer(){
		
		stopServer();
		startServer();
		
		System.out.println("Server restarted!");
		
	}
}
